import request from "@/utils/request";
import axios from "axios";

export function getActivityList(data) {
  return request({
    url: "/activity/list",
    method: "post",
    data,
  });
}

export function addActivity(data) {
  return request({
    url: "/activity/add",
    method: "post",
    data,
  });
}

export function removeActivity(data) {
  return request({
    url: "/activity/remove",
    method: "post",
    data,
  });
}

export function closeActivity(data) {
  return request({
    url: "/activity/close",
    method: "post",
    data,
  });
}

export function getActivity(data) {
  return request({
    url: "/activity/get",
    method: "get",
    params: data,
  });
}
export function getAppointmentList(data) {
  return request({
    url: "/appointment/get",
    method: "get",
    params: data,
  });
}

export function updateActivity(data) {
  return request({
    url: "/activity/update",
    method: "post",
    data,
  });
}
export function getMyActivity(data) {
  return request({
    url: "/activity/getMyActivity",
    method: "get",
    params: data
  });
}

export function getNotMyActivity(data) {
  return request({
    url: "/activity/getNotMyActivity",
    method: "get",
    params: data
  });
}

export function download(file) {
  const link = document.createElement("a");
  link.style.display = "none";
  const blob = new Blob([file.file], {
    type: "application/vnd.ms-excel"
  });
  // const blob = new Blob([file.file]);
  link.href = URL.createObjectURL(blob);
  link.setAttribute("download", file.name);
  document.body.appendChild(link);
  link.click();
  URL.revokeObjectURL(link.href);
  document.body.removeChild(link);
}

export function exportExcel(params = {}) {
  axios({
    url: "http://localhost:8082/activity/exportActivity",
    method: "get",
    params,
    responseType: "blob"
  }).then(res => {
    if (res.data) {
      const contentDisposition = res.headers["content-disposition"];
      if (contentDisposition) {
        const fileName = window.decodeURI(
          res.headers["content-disposition"].split("=")[1],
          "UTF-8"
        );
        download({
          name: fileName,
          file: res.data
        });
      }
    }
  });
}

